<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);

class manage extends admin{
	private $db,$linkage;
	public $siteid;
	function __construct() {
		parent::__construct();
		pc_base::load_app_func('global');
		$this->db = pc_base::load_model('resource_model');
		$this->siteid = get_siteid();
	}
	/**
	 * 资源列表
	 */
	public function init(){
		if ($_GET['bindtype'] == 1) {
			$id = $_GET['id'];//模特ID
			$db = pc_base::load_model('ss_member_model');
			$data = $db->get_one(array('id'=>$id));
		}
		$where = '';
		$where .= "relevance_id=".$id." AND bindtype=".$_GET['bindtype']." AND siteid = " . $this->siteid;
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		$resourcelist = $this->db->listinfo($where, 'id DESC', $page, 15);
		$pages = $this->db->pages;
		$show_header = true;
		pc_base::load_sys_class('format', '', 0);
		include $this->admin_tpl('resource_list');
	}
	/**
	 * 添加资源
	 */
	public function add() {
		if (isset($_POST['dosubmit']) && !empty($_POST['dosubmit'])) {
			if(trim($_POST['resource']['title']) == '') showmessage(L('title_cannot_empty'));
			if(trim($_POST['resource']['content']) == '') showmessage(L('content_cannot_empty'));
			//获取上传文件的后缀名
			$file_types = explode ( ".", $_POST['resource']['content'] );
			$file_type = $file_types [count ( $file_types ) - 1];
			$data['title'] = $_POST['resource']['title'];
			$data['description'] = $_POST['resource']['description'];
			$data['thumb'] = $_POST['resource']['thumb'];
			$data['content'] = $_POST['resource']['content'];
			$data['addusername'] = param::get_cookie('admin_username');
			$data['addtime'] = SYS_TIME;
			$data['adduserid'] = $_SESSION['userid'];
			$data['relevance_id'] = $_POST['resource']['catid'];
			if ($file_type == 'jpg' || $file_type == 'jpeg' || $file_type == 'gif' || $file_type == 'bmp' || $file_type == 'png') {
				$data['resource_type'] = 1;//图片类文件
			} elseif ($file_type == 'mp4' || $file_type == 'avi'){
				$data['resource_type'] = 2;//视频类文件
			} else {
				$data['resource_type'] = 3;//其他类文件
			}
			$data['bindtype'] = $_POST['resource']['bindtype'];
			$data['siteid'] = $this->siteid;
			$r_id = $this->db->insert($data,true);
			if ($r_id) {
				showmessage(L('success'),"?m=resource&c=manage&a=init&id=".$data['relevance_id']."&bindtype=".$data['bindtype']);
			}else {
				showmessage(L('error'));
			}
		}else {
			if ($_GET['bindtype'] == 1) {
				$id = $_GET['relevance_id'];//模特ID
				$db = pc_base::load_model('ss_member_model');
				$data = $db->get_one(array('id'=>$id));
			}
			pc_base::load_sys_class('form', '', 0);
			$show_header = true;
			include $this->admin_tpl('resource_add');
		}
	}
	/**
	 * 编辑资源
	 */
	public function edit() {
		$id = isset($_GET['resourceid']) ? $_GET['resourceid'] : showmessage(L('resource not exists'), HTTP_REFERER);
		$data = $this->db->get_one(array('id'=>$id));
		$relevance_id = $_GET['relevance_id'];
		$bindtype = $_GET['bindtype'];
		if (isset($_POST['dosubmit']) && !empty($_POST['dosubmit'])) {
			if(trim($_POST['resource']['title']) == '') showmessage(L('title_cannot_empty'));
			if(trim($_POST['resource']['content']) == '') showmessage(L('content_cannot_empty'));
			//获取上传文件的后缀名
			$file_types = explode ( ".", $_POST['resource']['content'] );
			$file_type = $file_types [count ( $file_types ) - 1];
			$data['title'] = $_POST['resource']['title'];
			$data['description'] = $_POST['resource']['description'];
			$data['thumb'] = $_POST['resource']['thumb'];
			$data['content'] = $_POST['resource']['content'];
			$data['addusername'] = param::get_cookie('admin_username');
			$data['updatetime'] = SYS_TIME;
			$data['adduserid'] = $_SESSION['userid'];
			$data['relevance_id'] = $_POST['resource']['catid'];
			if ($file_type == 'jpg' || $file_type == 'jpeg' || $file_type == 'gif' || $file_type == 'bmp' || $file_type == 'png') {
				$data['resource_type'] = 1;//图片类文件
			}elseif ($file_type == 'mp4' || $file_type == 'avi'){
				$data['resource_type'] = 2;//视频类文件
			} else {
				$data['resource_type'] = 3;//其他类文件
			}
			if ($this->db->update($data,array('id'=>$id,'bindtype'=>$bindtype))) {
				showmessage(L('update_success'),"?m=resource&c=manage&a=init&id=".$data['relevance_id']."&bindtype=".$bindtype);
			}else {
				showmessage(L('update_error'));
			}
		}else {
			if ($_GET['bindtype'] == 1) {
				$id = $_GET['relevance_id'];//模特ID
				$db = pc_base::load_model('ss_member_model');
				$result = $db->get_one(array('id'=>$id));
			}
			pc_base::load_sys_class('form', '', 0);
			$show_header = true;
			include $this->admin_tpl('resource_edit');
		}
	}
	
	/**
	 * 附件资源删除
	 */
	public function delete ()
	{
		if((!isset($_GET['id']) || empty($_GET['id'])) && (!isset($_POST['id']) || empty($_POST['id']))) {
			showmessage(L('illegal_parameters'), HTTP_REFERER);
		} else {
			if(is_array($_POST['id'])){
				foreach($_POST['id'] as $jobid_arr) {
					$this->db->delete(array('id'=>$jobid_arr));
				}
				showmessage(L('operation_success'),HTTP_REFERER);
			}else{
				$id = intval($_GET['id']);
				if($id < 1) return false;
				$result = $this->db->delete(array('id'=>$id));
				if($result){
					showmessage(L('operation_success'),HTTP_REFERER);
				}else {
					showmessage(L("operation_failure"),HTTP_REFERER);
				}
			}
		}
	}
}